/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.FasilitasDAO;
import com.ivanbiz.dao.ItemFasilitasDAO;
import com.ivanbiz.dao.impl.FasilitasDAOImpl;
import com.ivanbiz.dao.impl.ItemFasilitasDAOImpl;
import com.ivanbiz.model.Fasilitas;
import com.ivanbiz.model.ItemFasilitas;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.service.ServiceHelper;
import java.text.NumberFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class ItemFasilitasPanel extends javax.swing.JPanel {

    List<ItemFasilitas> listItemFasilitas;
    NumberFormat numberFormat;
    JDialog jDialogKamar;
    Kamar kamar;
    List<Fasilitas> listFasilitas;

    public ItemFasilitasPanel() {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        refresh();
    }

    public ItemFasilitasPanel(JDialog jDialog, List<Fasilitas> listFasilitas, Kamar kamar) {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        this.jDialogKamar = jDialog;
        this.kamar = kamar;
        this.listFasilitas = listFasilitas;
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableItemFasilitas = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonTambah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Daftar Fasilitas");

        tableItemFasilitas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableItemFasilitas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableItemFasilitasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableItemFasilitas);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah Fasiltas Baru");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonTambah);

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus Fasilitas Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });
        jPanel1.add(buttonHapus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        JDialog jDialog = new JDialog();
        jDialog.setModal(true);
        jDialog.setSize(435, 235);
        jDialog.add(new ItemFasilitasUpdatePanel(jDialog));
        jDialog.setLocationRelativeTo(null);
        jDialog.setVisible(true);
        refresh();
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableItemFasilitas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                ItemFasilitas itemFasilitas = listItemFasilitas.get(tableItemFasilitas.getSelectedRow());
                ItemFasilitasDAO itemFasilitasDAO = new ItemFasilitasDAOImpl();
                itemFasilitasDAO.delete(itemFasilitas);
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(ItemFasilitasPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed

    private void tableItemFasilitasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableItemFasilitasMouseClicked
        if (kamar != null) {
            if (evt.getClickCount() == 2) {
                int response = JOptionPane.showConfirmDialog(this, "Tambah Fasilitas ?", "Konfirmasi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                    try {
                        ItemFasilitas itemFasilitas = listItemFasilitas.get(tableItemFasilitas.getSelectedRow());
                        for (Fasilitas fasilitas : listFasilitas) {
                            if (fasilitas.getItemFasilitas().getId() == itemFasilitas.getId()) {
                                JOptionPane.showMessageDialog(this, "Item sudah ada pada daftar Fasilitas", "warning", JOptionPane.WARNING_MESSAGE);
                                return;
                            }
                        }
                        Fasilitas fasilitas = new Fasilitas();
                        fasilitas.setKamar(kamar);
                        fasilitas.setItemFasilitas(itemFasilitas);
                        FasilitasDAO fasilitasDAO = new FasilitasDAOImpl();
                        fasilitasDAO.saveOrUpdate(fasilitas);
                        jDialogKamar.dispose();
                    } catch (Exception ex) {
                        Logger.getLogger(ItemFasilitasPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }//GEN-LAST:event_tableItemFasilitasMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableItemFasilitas;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            ItemFasilitasDAO itemFasilitasDAO = new ItemFasilitasDAOImpl();
            listItemFasilitas = itemFasilitasDAO.getAll(ItemFasilitas.class);
            updateTableItemFasilitas();
        } catch (Exception ex) {
            Logger.getLogger(ItemFasilitasPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableItemFasilitas() {
        String[] judul = {"No", "Nama", "Tarif"};
        Object[][] isi = new Object[listItemFasilitas.size()][3];
        int x = 0;
        int no = 0;
        for (ItemFasilitas itemFasilitas : listItemFasilitas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = itemFasilitas.getNama();
            isi[x][2] = numberFormat.format(itemFasilitas.getTarif());
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableItemFasilitas);
    }
}
